/*

*/

DIVIDE:
  PUSH(FP);
  MOV(FP,SP);
  PUSH(R1); //save R1
  PUSH(R2); //save R2
  PUSH(R3); //save R2
  MOV(R1, FPARG(1)); //R1= n
  CMP(R1,1);
  JUMP_EQ(DIVIDE_1_ARG);
  JUMP_LT(ARG_N_ERROR);
  CMP(IND(FPARG(2)),T_INTEGER);
  JUMP_EQ(DIVIDE_INIT_INT);
  CMP(IND(FPARG(2)),T_FRACTION);
  JUMP_NE(TYPE_ERROR);
  PUSH(INDD(FPARG(2),2));
  PUSH(INDD(FPARG(2),1));
  CALL(MAKE_SOB_FRACTION);
  DROP(2);
  MOV(R2,R0);
  INCR(R1);
  JUMP(DIVIDE_LOOP);
DIVIDE_INIT_INT:

  PUSH(1);
  PUSH(INDD(FPARG(2),1));
  CALL(MAKE_SOB_FRACTION);
  DROP(2);
  MOV(R2,R0);
  INCR(R1);
DIVIDE_LOOP:
  MOV(R3,FPARG(R1));
  MUL(INDD(R2,2),INDD(R3,1)); //MULTIPLAY DENOMINATOR
  CMP(IND(R3),T_FRACTION);
  JUMP_NE(DIVIDE_LOOP_INT);
  MUL(INDD(R2,1),INDD(R3,2)); //MULTIPLAY NOMINATOR
  DECR(R1);
  CMP(R1,2);
  JUMP_GT(DIVIDE_LOOP);
  JUMP(DIVIDE_LOOP_EXIT);

DIVIDE_LOOP_INT:

  CMP(IND(R3),T_INTEGER);
  JUMP_NE(TYPE_ERROR2);
  DECR(R1);
  CMP(R1,2);
  JUMP_GT(DIVIDE_LOOP);
 
DIVIDE_LOOP_EXIT:
  PUSH(R2);
  CALL(MINIMIZE_FRAC);
  DROP(1);
  POP(R3);
  POP(R2);
  POP(R1);
  POP(FP);
  RETURN;
DIVIDE_1_ARG:

     CMP(IND(FPARG(2)),T_INTEGER);
     JUMP_EQ(DIVIDE_1_INT);
     CMP(IND(FPARG(2)),T_FRACTION);
     JUMP_NE(TYPE_ERROR);
     PUSH(INDD(FPARG(2),1));
     PUSH(INDD(FPARG(2),2));
     CALL(MAKE_SOB_FRACTION);
	DROP(2);
	PUSH(R0)
	CALL(MINIMIZE_FRAC);
	DROP(1);

	POP(R3);
	POP(R2);
	POP(R1);
	POP(FP);
	RETURN;
DIVIDE_1_INT:
	PUSH(INDD(FPARG(2),1));
	PUSH(1);
	CALL(MAKE_SOB_FRACTION);
	DROP(2);
	  POP(R3);
	  POP(R2);
	  POP(R1);
	POP(FP);
	RETURN;
